/*
   Copyright (c) 2011 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-03-05 Marco Riccardi - initial implementation
 *
 */
package org.mizartools.example.ui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.util.ArrayList;

import javax.swing.JInternalFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;

import org.mizartools.system.Error;
import org.mizartools.system.MessageFile;
import org.mizartools.utility.Html;

@SuppressWarnings("serial")
public class MessagesFrame extends JInternalFrame implements Runnable {

	private JTextPane textPane;
	private JPanel panel;
	public JScrollPane scroller;
	
	public MessagesFrame() {
    	super("Messages", true, true, true, true);
    	panel = new JPanel();
    	panel.setLayout(new BorderLayout());
		panel.setPreferredSize(new Dimension(500, 350));
		textPane = new JTextPane();
        scroller = new JScrollPane();
		scroller.getViewport().add(textPane);
		panel.add(scroller, BorderLayout.CENTER);
		getContentPane().add(panel, BorderLayout.CENTER);
		pack();
	}
	
	private String getHtml(){
    	StringBuilder html = new StringBuilder();
    	ArrayList<Error> errorList = MessageFile.getErrorList();
    	html.append("<html>");
    	html.append("<body>");
    	html.append("<table width=100% border=1>");
    	for (Error error : errorList){
			html.append("<tr>");
    		html.append("<td>");
        	html.append("<font face=\"verdana\"><b>");
        	html.append(error.getId());
        	html.append("</b></font");
    		html.append("</td>");
    		html.append("<td>");
        	html.append("<font face=\"verdana\"><b>");
        	html.append(Html.changeChars(error.getErrorMessage()));
        	html.append("</b></font");
    		html.append("</td>");
    		html.append("</tr>");
    	}
    	html.append("</table>");
    	html.append("</body>");
    	html.append("</html>");
    	return html.toString();
	}

	@Override
	public void run() {
		panel.setVisible(false);
		textPane.setContentType("text/html");
		textPane.setText(getHtml());
		textPane.setEditable(false);
		panel.setVisible(true);
	}
}
